#include "entity.h"

Entity::Entity()
{
	x = y = z = 0; 
	scale = 1.0f;
	moveRate = 1.0f;
	isAlive = false;
}

Entity::Entity(Game *prnt)
{
	parent = prnt;
	x = y = z = 0; 
	scale = 1.0f;
	moveRate = 1.0f;
	isAlive = true;
}

void Entity::Update()
{

}

void Entity::setTexture(dxManager* dx)
{
	//D3DXCreateTextureFromFile( pd3dDevice, "./arrows.bmp", &g_pTexture );
}

Entity::Entity(ID3DXMesh *mesh, D3DMATERIAL9 mtrl, float startX, float startY, float startZ)
{
	x = startX;
	y = startY;
	z = startZ;
	entMesh = mesh;
	entMtrl = mtrl;
	scale = 1.0f;
	moveRate = 5.0f;

	isAlive = false;
}

int Entity::draw()
{

	// Not drawing 'cause it's ded
	if(!this->isAlive)
		return 0;	

	LPDIRECT3DDEVICE9 temp = parent->pd3dDevice;

	//declare matrices for transformation
	D3DXMATRIX entMat, entScale, entTrans;

	D3DXMatrixIdentity(&entMat);

	//set scaling
	D3DXMatrixScaling(&entScale, scale, scale, scale);

	//set translation
	D3DXMatrixTranslation(&entTrans, x, y, z);
	D3DXMatrixMultiply(&entMat, &entMat, &entTrans);
	D3DXMatrixMultiply(&entMat, &entMat, &entScale);
	D3DXMatrixMultiply(&entMat, &entMat, parent->getView());

	temp->SetTransform(D3DTS_WORLD, &entMat);

	temp->SetMaterial(&entMtrl);

	entMesh->DrawSubset(0);

	stupidMatrix = entMat;
/*
	Entity* tempEnt = (Entity*)this;

	tempEnt->draw();
*/
	switch(type)
	{
	case ENT_PLAYER:
		Player* tempPlayer;
		tempPlayer = (Player*)this;
		tempPlayer->playerDraw();		
		break;
	case ENT_ENEMY:
		Enemy* tempEnemy;
		tempEnemy = (Enemy*)this;
		tempEnemy->enemyDraw();
		break;
	case ENT_LIGHT:
		Light* tempLight;
		tempLight = (Light*)this;
		tempLight->lightDraw();
		
		break;
	}

	return 0;
}

bool Entity::moveX(float amount)
{
	x += amount;
	return true;
}

bool Entity::moveY(float amount)
{
	y += amount;
	return true;
}

bool Entity::moveZ(float amount)
{
	z += amount;
	return true;
}

void Character::Update()
{

}

void Player::Update()
{
	if (parent->keyboard->isKeyHeld(KEY_UP))
		moveZ(moveRate);
	if (parent->keyboard->isKeyHeld(KEY_DOWN))
		moveZ(-moveRate);
	if (parent->keyboard->isKeyHeld(KEY_RIGHT))
		moveX(moveRate);
	if (parent->keyboard->isKeyHeld(KEY_LEFT))
		moveX(-moveRate);
}

void Player::playerDraw()
{
}

void Enemy::Update()
{

}

void Enemy::enemyDraw()
{

}

void Entity::setMesh(ID3DXMesh *mesh)
{
	entMesh = mesh;
}

void Entity::setMtrl(D3DMATERIAL9 mtrl)
{
	entMtrl = mtrl;
}

void Light::initLight(float red, float green, float blue)
{
	lightNum = parent->numLights;
	parent->numLights++;

	light.Type		= D3DLIGHT_POINT;

	light.Diffuse.r = light.Specular.r = light.Ambient.r = red;
	light.Diffuse.g = light.Specular.g = light.Ambient.g = green;
	light.Diffuse.b = light.Specular.b = light.Ambient.b = blue;
	light.Diffuse.a = light.Specular.a = light.Ambient.a = 1.0f;
	light.Position     = D3DXVECTOR3( 0.0f, 0.0f, -1.0f );		
	light.Attenuation0	= light.Attenuation1 = light.Attenuation2 = 0.015f;
	
	light.Range        = 10000;
	
	parent->pd3dDevice->SetLight(lightNum, &light );
	parent->pd3dDevice->LightEnable(lightNum, TRUE );

}

void Light::Update()
{
	static float i = 0;
	float add;

	if(parent->rave)
	{
		i++;
		add = -sin(i/4)*2;

		z += add;
	}
}

void Light::lightDraw()
{
	light.Position = D3DXVECTOR3(stupidMatrix._41, stupidMatrix._42, stupidMatrix._43);
	parent->pd3dDevice->SetLight(lightNum, &light);
}